<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au">

  <xsl:import href='common.xsl'/>

  <db:article>
    <db:info>
      <db:title>AMT Extract File</db:title>

      <db:copyright>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>1.41</db:revnumber>
          <db:date>2012-11-07</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added documentation on the output format.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.4</db:revnumber>
          <db:date>2012-10-31</db:date>
          <db:authorinitials>SB</db:authorinitials>
          <db:revremark>Updated to PBS XML Schema v2.8.1</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.3</db:revnumber>
          <db:date>2011-11-14</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Modified keys to address performance issues</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.2</db:revnumber>
          <db:date>2011-10-19</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Updated to PBS XML v2.6.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.1</db:revnumber>
          <db:date>2011-06-02</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated to new specification.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2011-05-09</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet produces an extract file (<db:filename>amt yyyymmdd.txt</db:filename>) from a PBS XML document containing details of AMT identifiers for PBS items.</db:para>
    <db:para>The source document must be a PBS XML document.</db:para>
    <db:para>The result document is a text file.</db:para>
    <db:para>The default field delimeter in this file is the exclamation mark (!).</db:para>
    <db:table>
      <db:tgroup cols="4" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Program Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>2</db:entry>
            <db:entry>As per Section 10  Drug Type Codes.</db:entry>
          </db:row>
          <db:row>
            <db:entry>PBS Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>6</db:entry>
            <db:entry>Five or six alphanumeric characters in format (n)nnnna.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Manufacturer Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Two character alphabetic, refer to file Mnfr yyyymmdd.txt. Two character alphabetic code in the Schedule Book.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Unit of Measure</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>E.g. Milligram, Micrograms, International Units.</db:entry>
          </db:row>
          <db:row>
            <db:entry>MP Concept ID</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Medicinal product Code.</db:entry>
          </db:row>
          <db:row>
            <db:entry>MP</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Medicinal Product.</db:entry>
          </db:row>
          <db:row>
            <db:entry>MPP Concept ID</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Medicinal Product Pack Code.</db:entry>
          </db:row>
          <db:row>
            <db:entry>MPP</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Medicinal Product Pack.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Brand Name</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>TPP's 'Brand Name' as used in the PBS.</db:entry>
          </db:row>
          <db:row>
            <db:entry>TPP Concept ID</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Trade Product Pack Code.</db:entry>
          </db:row>
          <db:row>
            <db:entry>TPP</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Trade Product Pack.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Pack Content</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>The number of UOM's in the manufacturer's pack (In the single vial).</db:entry>
          </db:row>
          <db:row>
            <db:entry>Max Quantity</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Max Amount of Medicinal Product in UOM's which may be prescribed without obtaining a special or additional authority.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Commonwealth's Ex-Manufacturer Price</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Commonwealth's 'Ex-Manufacturer Price' for a manufacturer's pack.</db:entry>
          </db:row>
          <db:row>
            <db:entry>Manufacturer's Ex-Manufacturer Price</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Manufacturer's 'Ex-Manufacturer Price' for a manufacturer's pack.</db:entry>
          </db:row>
          <db:row>
            <db:entry>PFDI Amount</db:entry>
            <db:entry>Character</db:entry>
            <db:entry></db:entry>
            <db:entry>Value of Premium Free Dispensing Incentive.</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
  </db:article>

  <xsl:output method='text'/>

  <db:param>Whether to display header.</db:param>
  <xsl:param name='include-header' select='0'/>

  <db:param>What character to use to separate the data fields.</db:param>
  <xsl:param name='delimiter' select='"!"'/>

  <xsl:key name='tppID' match='/pbs:root/pbs:drugs-list/pbs:mp/pbs:mpp/pbs:tpp' use='concat("#", @xml:id)'/>

  <xsl:key name='orgID' match='/pbs:root/pbs:organisations-list/pbs:organisation' use='concat("#", @xml:id)'/>
 
  <db:template name='root'>
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML Schema.</db:para>
  </db:template>
  <xsl:template match='/'>
    <xsl:variable name='major' select='substring-before(/*/@version, ".")'/>

    <xsl:choose>
      <xsl:when test='$major >= 2'>
        <xsl:if test='$include-header = 1'>
          <xsl:text>Program code</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>Item code</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>Manufacturer code</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>Unit of measure</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>MP ID</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>MP PT</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>MPP ID</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>MPP PT</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>Brand name</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>TPP ID</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>TPP PT</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>Pack content</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>Max qty</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>CEMP</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>MEMP</xsl:text>
          <xsl:value-of select='$delimiter'/>
          <xsl:text>PFDI</xsl:text>
          <xsl:text>&#xa;</xsl:text>
        </xsl:if>
        <xsl:apply-templates
          select='/pbs:root/pbs:schedule/pbs:listings-list/pbs:prescribing-rule[not(self::pbs:solvent-rule|self::pbs:extemporaneous-preparation|self::pbs:standard-formula-preparation|self::pbs:drug-tariff)]'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate='yes'>This stylesheet is designed to operate upon a Schedule XML document that conforms to version 2.5 (or later) of the PBS XML Schema.  See http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of this XSL stylesheet that operates on documents that conform to an earlier version of the schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='pbs:prescribing-rule'>
    <xsl:choose>
      <xsl:when test='pbs:infusible'>
        <xsl:apply-templates select='*/pbs:pricing/pbs:reimbursement/pbs:prices/pbs:tpp-list/pbs:tpp-reference'>
          <xsl:with-param name='pr' select='.'/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select='*/pbs:pricing/pbs:prices/pbs:tpp-reference'>
          <xsl:with-param name='pr' select='.'/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='pbs:tpp-reference'>
    <xsl:param name='pr' select='/..'/>

    <xsl:variable name='tpp' select='key("tppID", @xlink:href)'/>
    <xsl:variable name='org' select='key("orgID", $tpp/pbs:organisation-reference/@xlink:href)'/>

    <!-- Program code (Drug Type code) -->
    <xsl:apply-templates select='$pr/ancestor::pbs:listings-list/pbs:info/pbs:code' mode='textonly'/>
    <xsl:value-of select='$delimiter'/>
    <!-- PBS Item Code -->
    <xsl:apply-templates select='$pr/pbs:code' mode='textonly'/>
    <xsl:value-of select='$delimiter'/>
    <!-- Manufacturer's code -->
    <xsl:apply-templates select='$org/pbs:code' mode='textonly'/>
    <xsl:value-of select='$delimiter'/>
    <!-- Unit of Measure -->
    <xsl:if test='$pr/*/pbs:maximum-quantity/@reference = "unit-of-measure"'>
      <xsl:apply-templates select='$pr/*/pbs:maximum-quantity/@amount' mode='textonly'/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates select='$pr/*/pbs:maximum-quantity/@unit' mode='textonly'/>
    </xsl:if>
    <xsl:value-of select='$delimiter'/>
    <!-- AMT MP -->
    <xsl:apply-templates select='$tpp/../../pbs:code[@scheme = "urn:snomed-org/sct"]'
      mode='textonly'/>
    <xsl:value-of select='$delimiter'/>
    <xsl:apply-templates select='$tpp/../../db:title' mode='textonly'/>
    <xsl:value-of select='$delimiter'/>
    <!-- AMT MPP -->
    <xsl:choose>
      <xsl:when test='$pr/pbs:infusible'>
        <xsl:apply-templates select='$tpp/../pbs:mpuu/pbs:code[@scheme = "urn:snomed-org/sct"]' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
        <xsl:apply-templates select='$tpp/../pbs:mpuu/db:title' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select='$tpp/../pbs:code[@scheme = "urn:snomed-org/sct"]'
          mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
        <xsl:apply-templates select='$tpp/../db:title' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
      </xsl:otherwise>
    </xsl:choose>
    <!-- Brand name -->
    <xsl:apply-templates select='$tpp/db:subtitle' mode='textonly'/>
    <xsl:value-of select='$delimiter'/>
    <!-- AMT TPP -->
    <xsl:choose>
      <xsl:when test='$pr/pbs:infusible'>
        <xsl:apply-templates select='$tpp/pbs:tpuu/pbs:code[@scheme = "urn:snomed-org/sct"]' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
        <xsl:apply-templates select='$tpp/pbs:tpuu/db:title' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select='$tpp/pbs:code[@scheme = "urn:snomed-org/sct"]' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
        <xsl:apply-templates select='$tpp/db:title' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
      </xsl:otherwise>
    </xsl:choose>
    <!-- Pack content -->
    <xsl:apply-templates select='$tpp/pbs:vial-content' mode='textonly'/>
    <xsl:value-of select='$delimiter'/>
    <!-- Max quantity -->
    <xsl:choose>
      <xsl:when test='$pr/*/pbs:maximum-quantity[@reference = "unit-of-measure"]'>
        <xsl:apply-templates select='$pr/*/pbs:maximum-quantity[@reference = "unit-of-measure"]' mode='textonly'/>
      </xsl:when>
      <xsl:when test='$pr/*/pbs:maximum-quantity[@reference = "unit-of-use"]'>
        <xsl:apply-templates select='$pr/*/pbs:maximum-quantity[@reference = "unit-of-use"]' mode='textonly'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select='$pr/*/pbs:maximum-quantity[1]' mode='textonly'/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select='$delimiter'/>
    <!-- Reimbursement EMP for TPP -->
    <xsl:choose>
      <xsl:when test='$pr/pbs:infusible'>
        <xsl:apply-templates
          select='$pr/*/pbs:pricing/pbs:reimbursement/pbs:prices/pbs:tpp-list[pbs:tpp-reference[@xlink:href = current()/@xlink:href]]/pbs:tpuu-ex-manufacturer'
          mode='textonly'/>
      </xsl:when>
      <xsl:when test='starts-with($pr/ancestor::pbs:listings-list/pbs:info/pbs:pricing-model, "http://schema.pbs.gov.au/Pricing/ready-prepared-no-premium")'>
        <!-- Reimbursement price is the manufacturer price -->
        <xsl:apply-templates select='$tpp/pbs:ex-manufacturer' mode='textonly'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select='$pr/*/pbs:pricing/pbs:reimbursement/pbs:ex-manufacturer'
          mode='textonly'/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select='$delimiter'/>
    <!-- Manufacturer EMP for TPP -->
    <xsl:choose>
      <xsl:when test='$pr/pbs:infusible'>
        <xsl:apply-templates select='$tpp/pbs:tpuu-ex-manufacturer' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select='$tpp/pbs:ex-manufacturer' mode='textonly'/>
        <xsl:value-of select='$delimiter'/>
      </xsl:otherwise>
    </xsl:choose>
    <!-- PFDI -->
    <xsl:choose>
      <xsl:when test='$pr/pbs:infusible'>
        <xsl:apply-templates
          select='$pr/*/pbs:pricing/pbs:manufacturer/pbs:prices[pbs:tpp-list/pbs:tpp-reference[@xlink:href = current()/@xlink:href]]/pbs:incentives-list/pbs:incentive[pbs:type = "http://schema.pbs.gov.au/Incentive#dispensing" or pbs:type = "incentive:dispensing"]/pbs:amount[1]'
          mode='textonly'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates
          select='$pr/*/pbs:pricing/pbs:prices[pbs:tpp-reference[@xlink:href = current()/@xlink:href]]/pbs:incentives-list/pbs:incentive[pbs:type = "http://schema.pbs.gov.au/Incentive#dispensing" or pbs:type = "incentive:dispensing"]/pbs:amount[1]'
          mode='textonly'/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:text>&#xa;</xsl:text>

  </xsl:template>

</xsl:stylesheet>
